package com.yunzhong.appointment.personal.controller;

import com.yunzhong.appointment.config.Const;
import com.yunzhong.appointment.config.SessionConst;
import com.yunzhong.appointment.entity.*;
import com.yunzhong.appointment.illnesstpye.entity.ResultJson;
import com.yunzhong.appointment.personal.service.personalService;
import com.yunzhong.appointment.util.FileUtils;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.List;

@Controller
public class personalController {
    @Autowired
    private personalService personalService;
    @Autowired
    private FileUtils fileUtils;

    /**
     * 显示头像
     * @param request
     * @param modelMap
     * @return
     */
    @RequestMapping("/personal/head")
    public String head(HttpServletRequest request, ModelMap modelMap) {
//        String id = (String) request.getSession().getAttribute("id");
//        Patient pat = personalService.queryByName(id);
//        modelMap.addAttribute("pat",pat);

        SysUser user = (SysUser) SecurityUtils.getSubject().getSession().getAttribute(SessionConst.SESSION_USER);
        if(SecurityUtils.getSubject().hasRole(Const.ROLE_PATIENT)){
            Patient pat = personalService.queryByName(user.getUserId());
            System.out.println("=================="+pat);
            modelMap.put("pat", pat);
        }else{
            System.out.println(user.getUserId());
            Person person = null;
            modelMap.put("person", person);
        }

        return "personal/personPhoto";
    }

    /**
     * 默认显示个人信息
     * @param request
     * @param modelMap
     * @return
     */
    @RequestMapping("/personal/querypersonal")
    public String queryPer(HttpServletRequest request, ModelMap modelMap) {
        SysUser user = (SysUser) SecurityUtils.getSubject().getSession().getAttribute(SessionConst.SESSION_USER);
        if (SecurityUtils.getSubject().hasRole(Const.ROLE_PATIENT)) {
//            String id = (String) request.getSession().getAttribute("id");
            Patient pat = personalService.queryByName(user.getUserId());
            System.out.println("pat=========="+pat);
            List<Province> listp = personalService.queryByPro();
            StringBuilder tel = personalService.queryByTel(pat);
            StringBuilder ide = personalService.queryByIde(pat);

            modelMap.addAttribute("listp", listp);
            modelMap.addAttribute("pat",pat);
            modelMap.addAttribute("tel",tel);
            modelMap.addAttribute("ide",ide);
        }else {
            Person person=null;
            modelMap.addAttribute("person", person);
        }

        return "personal/personal11";
    }

    /**
     * 转到个人中心
     * @return
     */
    @RequestMapping("/personal/personal")
    public String personal(){
        return "personal/personal";
    }

    @RequestMapping("/personal/query")
    public String queryPe(HttpServletRequest request, ModelMap modelMap) {

        String id = (String) request.getSession().getAttribute("id");
        System.out.println("555555555555555"+id);
        Patient pat = personalService.queryByName(id);
        List<Province> listp = personalService.queryByPro();
        StringBuilder tel = personalService.queryByTel(pat);
        StringBuilder ide = personalService.queryByIde(pat);

        modelMap.addAttribute("listp", listp);
        modelMap.addAttribute("pat",pat);
        modelMap.addAttribute("tel",tel);
        modelMap.addAttribute("ide",ide);
        return "personal/personal11";
    }


    /**
     * 查找省
     * @param data
     * @return
     */
    @RequestMapping("/personal/queryPro")
    @ResponseBody
    public List<Province> queryPro(String data){
        List<Province> listp = personalService.queryByPro();
        return listp;
    }


    /**
     * 查找市
     * @param id
     * @return
     */
    @RequestMapping("/personal/queryCity")
    @ResponseBody
    public List<City> queryCity(String id) {
        List<City> listc = personalService.queryByCity(id);
        return listc;
    }

    /**
     * 查找区
     * @param id
     * @return
     */
    @RequestMapping("/personal/queryArea")
    @ResponseBody
    public List<Area> queryArea(String id) {
        List<Area> lista = personalService.queryByArea(id);
        return lista;
    }

    /**
     * 保存个人信息
     * @param patient
     * @return
     */
    @RequestMapping("/personal/editsave")
    @ResponseBody
    public ResultJson editsave(Patient patient) {
        int num = personalService.edit(patient);
        return ResultJson.out(num);
    }

    /**
     * 修改保存头像
     * @param patient
     * @param picFile
     * @return
     * @throws IOException
     */
    @RequestMapping("/personal/edithead")
    @ResponseBody
    public ResultJson editsave(Patient patient,@RequestParam("pic") MultipartFile picFile) throws IOException {
        if (picFile.getSize() > 0) {
            Patient hy = personalService.queryById(patient.getPatientId());
            String fileName = fileUtils.saveFile(picFile);
            System.out.println("fileName = " + fileName);
            String hpic = hy.getPatientPic();
            patient.setPatientPic(fileName);
            int n = personalService.edit(patient);
            if (n>0) {
                fileUtils.deleteFileByUrl(hpic);
            }else{
                fileUtils.deleteFileByUrl(fileName);
            }
        }
        int num = personalService.edit(patient);
        return ResultJson.out(num);

    }

}
